package com.msi.shortwave;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class frequencyadaptor  extends ArrayAdapter<frequency> {
	
	
	
    private ArrayList<frequency> items;
	private Context context;
	ViewHolder holder;
	private results resholder;
	

	
	
    public frequencyadaptor(Context context, int textViewResourceId, ArrayList<frequency> items, results results) {
            super(context, textViewResourceId, items);
            this.context = context;
            this.items = items;
            this.resholder = results;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
    	
		
            View v = convertView;
            holder = new ViewHolder();
            if (v == null) {
            	LayoutInflater vi = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
                v = vi.inflate(R.layout.row, null);
                holder = new ViewHolder();
                holder.sn = (TextView) v.findViewById(R.id.tvStationname);
        		holder.st = (TextView) v.findViewById(R.id.tvStarttime);
                holder.et = (TextView) v.findViewById(R.id.tvEndtime);
                holder.tl = (TextView) v.findViewById(R.id.tvTarget);
                holder.fl = (TextView) v.findViewById(R.id.tvFreqs);
                holder.is = (ImageView) v.findViewById(R.id.ImageSearch);
                holder.lb = (ImageView) v.findViewById(R.id.ImageLog);


                v.setTag(holder);            

            }else{
            	
            	holder = (ViewHolder) convertView.getTag();
            }
            frequency o = items.get(position);
            if (o != null) {
            		  if(o.getTargets().length() < 1 || o.getFreqs().contains("()") || !o.getFreqs().contains("(")) {
            			  holder.is.setVisibility(View.GONE);
            		  }else{
            			  holder.is.setVisibility(View.VISIBLE);
            		  }
                          holder.sn.setText(o.getStationname());                            }
                          holder.st.setText(o.getStarttime());
                        holder.et.setText(o.getEndtime());
                        holder.tl.setText(o.getTargets());
                        holder.fl.setText(o.getFreqs());
                  
                        ((View) holder.lb).setOnClickListener(new OnClickListener() {

                        	   @Override
                        	   public void onClick(View v) {
                        	    // TODO Auto-generated method stub
                        		   //results.test(position);
                        		   resholder.logclick(position);
                        	   }
                        	  });
                        
                        ((View) holder.is).setOnClickListener(new OnClickListener() {

                     	   @Override
                     	   public void onClick(View v) {
                     	    // TODO Auto-generated method stub
                     		  resholder.mapclick(position);
                     	   
                     	   }
                     	  });
           
            return v;
           
    }
    
    static class ViewHolder {            
        public Object lb;
		ImageView is;
		TextView sn;  
        TextView st;  
        TextView et;  
        TextView tl;  
        TextView fl;  
              
    } 
}